package com.aurora.source.util.encryption;

import android.util.Log;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author: Lance
 * @description: MD5加密 工具类
 * @date: 2018/7/6 14:010
 */
public class MD5Utils {

    public static String MD5(String string) {

            byte[] hash;
            try {
                hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
            } catch (NoSuchAlgorithmException e) {
                throw new RuntimeException("Huh, MD5 should be supported?", e);
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException("Huh, UTF-8 should be supported?", e);
            }

            StringBuilder hex = new StringBuilder(hash.length * 2);
            for (byte b : hash) {
                if ((b & 0xFF) < 0x10) hex.append("0");
                hex.append(Integer.toHexString(b & 0xFF));
            }
            return hex.toString();
        }


    // 最新MD5加密
    public static String encrypt16(String encryptStr){
        Log.e("sign",encryptStr);
        MessageDigest digest= null;
        try {
            digest = MessageDigest.getInstance("MD5");
            //使用指定的byte数组进行摘要
            digest.update(encryptStr.getBytes());
            //获取摘要的结果
            byte[] result = digest.digest();
            StringBuffer buffer=new StringBuffer();
            for (int i = 0; i < result.length; i++) {
                //把二进制的转化为十六进制
                String index= Integer.toHexString(result[i]&0xFF);
                //如果长度只有一位那么就补一个0
                if(index.length()==1) {
//                        index="0"+index;
                    buffer.append("0");
                }
                buffer.append(index);
            }
            encryptStr= buffer.toString();

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return encryptStr;
    }


}
